<?php
namespace Fwadmin\Controller;
use Think\Controller;
class CurdController extends Controller{
	public function index(){
		$this		-> display();
	}
	public function add(){
		if(IS_AJAX){
			$class = trim(I("class"));
			$Data = D($class);
			if($Data -> create()){
				$result = $Data -> add();
				if($result !== false){
					if($class == "Category"){
						$date['nav'] = nav();
						if(I('model_id') == 1){
							$page['cid'] = time();
							$page['category_id'] = $result;
							$page['title'] = I('name');
							$page['content'] = I('name');
							$page['last_time'] = date('Y-m-d H:i:s');
							M("Page") -> add($page);
						}
					}
					$date['msg']	= '添加成功！';
					$date['status']	= '1';
					switch(I('model_id')){
						case 1:
							$date['callback']	= U($class.'/index',array('category_id' => I('category_id')));
						break;
						case 2:
							$date['callback']	= U($class.'/index',array('category_id' => I('category_id')));
						break;
						case 3:
							$date['callback']	= U($class.'/index',array('category_id' => I('category_id')));
						break;
						default:
							$date['callback']	= U($class.'/index');
					}
					$this ->ajaxReturn($date);
				}else{
					$date['msg']	= '添加失败！';
					$date['status']	= '0';
					$this ->ajaxReturn($date);
				}
			}else{
				$date['msg']	= $Data->getError();
				$date['status']	= '0';
				$this ->ajaxReturn($date);
			}
		}
	}
	public function save(){
		if(IS_AJAX){
			$class	= trim(I("class"));
			$flag	= trim(I("flag"));
			$id		= trim(I("id"));
			$category_id = trim(I("category_id"));
			$Data	= D($class);
			if($Data -> create()){
				if($Data -> save() !== false){
					if($class == "Category"){
						$date['nav'] = nav();
						if(I('model_id') == 1){
							if(M('Page') -> where(array('category_id' => $id)) -> find() == false){
								$page['cid'] = time();
								$page['category_id'] = $id;
								$page['title'] = I('name');
								$page['content'] = I('name');
								$page['last_time'] = date('Y-m-d H:i:s');
								M("Page") -> add($page);
							}
						}else{
							M("Page") -> where(array('category_id' => $id)) -> delete(); 
						}
					}
					switch(I('model_id')){
						case 1:
							$date['callback']	= U($class.'/index',array('category_id' => $category_id));
						break;
						case 2:
							$date['callback']	= U($class.'/index',array('category_id' => $category_id));
						break;
						case 3:
							$date['callback']	= U($class.'/index',array('category_id' => $category_id));
						break;
						default:
							$date['callback']	= U($class.'/index');
					}
					$date['msg']	= '修改成功！';
					$date['status']	= '1';
					$this ->ajaxReturn($date);
				}else{
					$date['msg']	= '修改失败！';
					$date['status']	= '0';
					$this ->ajaxReturn($date);
				}
			}else{
				$date['msg']	= $Data->getError();
				$date['status']	= '0';
				$this ->ajaxReturn($date);
			}
		}
	}
	public function delete(){
		if(IS_AJAX){
			$class = I('class');
			$id = I('id');
			if(is_array($id)){
				$whereId = 'id in('.implode(',',$id).')';
				foreach($id as $value){
					$Data = M($class) -> where(array('id' => $value)) -> find();
					photoDelete($Data['cid']);
					@unlink('.'.$Data['thumb']);
					@unlink('.'.$Data['image']);
				}
			}else{
				$whereId = 'id='.$id;
				$Data = M($class) -> where(array('id' => $id)) -> find();
				photoDelete($Data['cid']);
				@unlink('.'.$Data['thumb']);
				@unlink('.'.$Data['image']);
			}
			$result = M($class) -> where($whereId) -> delete();
			if($result !== false){
				$date['msg']	= '删除成功！';
				$date['status']	= '1';
				$this ->ajaxReturn($date);
			}else{
				$date['msg']	= '删除失败！';
				$date['status']	= '0';
				$this ->ajaxReturn($date);
			}
		}
	}
	public function mobile(){
		if(IS_AJAX){
			$class = I('class');
			$id = I('id');
			$category_id = I('category_id');
			$whereId = 'id in('.implode(',',$id).')';
			foreach($id as $value){
				$data = array('category_id'=>$category_id,'last_time'=>time());
				M($class) -> where(array('id' => $value)) -> setField($data);
			}
			$date['msg']	= '移动成功！';
			$date['status']	= '1';
			$this ->ajaxReturn($date);
		}
	}
	public function search(){
		$model_id			= trim(I("model_id"));
		$map['model_id']	= array(1,$model_id,'or');
		$category			= M('Category') -> where($map) -> select();
		$category			= toLevel($category,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',0);
		$data['id']		= trim(I('category_id'));
		$getChildsId	= getChildsId($category,$data['id'],$data['id']);
		$this			-> ajaxReturn($getChildsId);
	}
	public function category(){
		if(IS_AJAX){
			$Data			= M('Category');
			$category		= $Data -> select();
			$data['id']		= trim(I('category_id'));
			$hasChild		= hasChild($category,$data['id']);
			switch($data['id']){
				case 0:
					$date['status']		= '1';
					$this				->ajaxReturn($date);
				break;
				case -1:
					$date['msg']	= '请选择所属栏目！';
					$date['status']	= '0';
					$this			->ajaxReturn($date);
				break;
				default:
					if($hasChild == true){
						$date['msg']		= '父栏目不允许添加文章';
						$date['status']		= '0';
						$this				-> ajaxReturn($date);
					}else{
						$date['status']		= '1';
						$this				->ajaxReturn($date);
					}		
			}
		}
	}
	public function categoryDel(){
		if(IS_AJAX){
			$Data			= M('category');
			$id				= I('id');
			$parent_id		= $Data -> where(array('id' => $id)) -> find();
			$If_parent_id	= $Data -> where(array('parent_id' => $id)) -> find();
			if($If_parent_id){
				$date['msg']	= '当前栏目下有子栏目，无法删除！';
				$date['status']	= '0';
				$this->ajaxReturn($date);
			}else{
				if($Data -> where($whereId) -> delete() !== false){
					M("Page") -> where(array('category_id' => $id)) -> delete(); 
					$date['msg']	= '删除成功！';
					$date['status']	= '1';
					$date['nav']	= nav();
					$this ->ajaxReturn($date);
				}else{
					$date['msg']	= '删除失败！';
					$date['status']	= '0';
					$this ->ajaxReturn($date);
				}
			}
		}
	}
	public function logout(){
		if(IS_AJAX){
			cookie('yz_name',null);
			$date['msg']	= '退出成功！';
			$date['status']	= '1';
			$this -> ajaxReturn($date);
		}
	}
}